package com.example.iread.http;

import com.example.iread.application.MyApplication;
import com.example.iread.bean.ResponseBean;
import com.example.iread.util.HttpUtil;

import java.util.concurrent.ThreadPoolExecutor;
import java.util.function.Consumer;

public class BookApi {

    private static final ThreadPoolExecutor threadPoolExecutor = MyApplication.getInstance().threadPoolExecutor;


    public static ResponseBean getByTypeId(int pageNum, int pageSize, int typeId) {
        return HttpUtil.doGet(String.format("/api/book/getBookList?pageNum=%s&pageSize=%s&typeId=%s", pageNum, pageSize, typeId));
    }

    public static void getByTypeId(int pageNum, int pageSize, int typeId, Consumer<ResponseBean> consumer) {
        threadPoolExecutor.execute(() -> consumer.accept(getByTypeId(pageNum, pageSize, typeId)));
    }

    public static ResponseBean getById(int bookId) {
        return HttpUtil.doGet("/api/book/getBookInfoById/" + bookId);
    }

    public static void getById(int bookId, Consumer<ResponseBean> consumer) {
        threadPoolExecutor.execute(() -> consumer.accept(getById(bookId)));
    }

    public static ResponseBean getSuggestionList(int age) {
        return HttpUtil.doGet("/api/book/recommended?age=" + age);
    }

    public static void getSuggestionList(int age, Consumer<ResponseBean> consumer) {
        threadPoolExecutor.execute(() -> consumer.accept(getSuggestionList(age)));
    }

    public static ResponseBean search(String keyword) {
        return HttpUtil.doGet("/api/book/search?keyWords=" + keyword);
    }

    public static void search(String keyword, Consumer<ResponseBean> consumer) {
        threadPoolExecutor.execute(() -> consumer.accept(search(keyword)));
    }


}
